using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PlugNT.Safe;
using PlugNT.Database.Common;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class FriendLinkClass
    {


        public static readonly string currTableName = WebConfig.TablePrefix + "friendlink_class";







        public FriendLinkClassInfo GetFriendLinkClassInfo(int id)
        {

            FriendLinkClassInfo model = null;
            IDataReader dr = GetReader(1, "id,[name]", " and id=" + id);
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new FriendLinkClassInfo();
                    model.sys_model_id = int.Parse(dr["sys_model_id"].ToString());
                    model.id = int.Parse(dr["id"].ToString());
                    model.name = dr["name"].ToString();
                }
                dr.Close();
            }
            return model;
        }











        public bool Add(FriendLinkClassInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName);
            strSql.Append(" values (");
            strSql.Append(model.sys_model_id.ToString() + ",");
            strSql.Append("'" + StringHelper.SqlFilter(model.name) + "'");
            strSql.Append(")");

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }






        public bool Update(FriendLinkClassInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName);
            strSql.Append(" set name='" + StringHelper.SqlFilter(model.name) + "'");
            strSql.Append(" where id=" + model.id);

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }






        public bool Delete(int id)
        {

            FriendLink.RemoveLinks(id);

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " +  currTableName);
            strSql.Append(" where id= " + id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }











        private IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }


        public DataTable GetList(int sysModelId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,name from " + currTableName + " where sys_model_id=" + sysModelId);
            return DbHelper.GetDataTable(strSql.ToString());
        }



    }
}

